/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.hanghoaDAO;
import dao.khohangDAO;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.hibernate.HibernateException;
import pojo.Hanghoa;
import pojo.Khohang;

/**
 *
 * @author HoaDinhGiang
 */
public class ThongKeHangTonKhoInternalFrame extends javax.swing.JInternalFrame {

    /**
     * Creates new form ThongKeHangTonKhoInternalFrame
     */
    private List<Hanghoa> dsHangHoa;
    private DefaultTableModel dtm;
    private DefaultComboBoxModel comboBoxModelKhoHang;
    private int tongHangTon;
    public void loadcomboxKhoHang() {
        try {
            this.comboBoxModelKhoHang = new DefaultComboBoxModel();
            this.jcbKhoHang.setModel(comboBoxModelKhoHang);
            List<Khohang> ds = khohangDAO.layDanhSachKhoHang();
            for (int i = 0; i < ds.size(); i++) {
                Khohang dm = ds.get(i);
                this.comboBoxModelKhoHang.addElement(dm);
            }
        } catch (HibernateException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    public ThongKeHangTonKhoInternalFrame() {
        initComponents();
        dtm = new DefaultTableModel();
        dtm.addColumn("Mã");
        dtm.addColumn("Tên Sản Phẩm");
        dtm.addColumn("Loại Sản Phẩm");
        dtm.addColumn("Đơn Vị");
        dtm.addColumn("Giá Bán");
        dtm.addColumn("Giá Tối Thiểu");
        dtm.addColumn("Giá Tối Đa");
        dtm.addColumn("Nhà Phân Phối");
        dtm.addColumn("Nhà Sản Xuất");
        dtm.addColumn("Kho");
        dtm.addColumn("Hàng Tồn");
        
    }

    public void hienThiHangTonTheoKhoHang() {

        jtbDanhSachHangHoa.removeAll();
        Khohang kh =  (Khohang) jcbKhoHang.getItemAt(jcbKhoHang.getSelectedIndex());
        int maKho =kh.getMakhohang();
        dsHangHoa = hanghoaDAO.layDanhSachHangTonCuaKhoHang(maKho);
        //Tạo model cho Table

        this.tongHangTon = 0;
        dtm.setNumRows(dsHangHoa.size());
        for (int i = 0; i < dsHangHoa.size(); i++) {
            Hanghoa hh = dsHangHoa.get(i);
            dtm.setValueAt(hh.getMahanghoa(), i, 0);
            dtm.setValueAt(hh.getTenhanghoa(), i, 1);
            dtm.setValueAt(hh.getLoaihanghoa().getTenloaihanghoa(), i, 2);
            dtm.setValueAt(hh.getDonvitinh().getTendonvitinh(), i, 3);
            dtm.setValueAt(hh.getGiaban(), i, 4);
            dtm.setValueAt(hh.getGiatoithieu(), i, 5);
            dtm.setValueAt(hh.getGiatoida(), i, 6);
            dtm.setValueAt(hh.getNhaphanphoi().getTennhaphanphoi(), i, 7);
            dtm.setValueAt(hh.getNhasanxuat().getTennhasanxuat(), i, 8);
            dtm.setValueAt(hh.getKhohang().getTenkhohang(), i, 9);
            dtm.setValueAt(hh.getHangton(), i, 10);
            this.tongHangTon+=hh.getHangton();
        }
        jlbtongHangTon.setText(String.valueOf(this.tongHangTon));
        //Hiển thị lên Table
        this.jtbDanhSachHangHoa.setModel(dtm);
        this.jtbDanhSachHangHoa.changeSelection(0, 0, false, false);

    }
    public void hienThiHangHoa() {

        jtbDanhSachHangHoa.removeAll();
        dsHangHoa = hanghoaDAO.layDanhSachHangTon();
        //Tạo model cho Table
        
        this.tongHangTon = 0;
        dtm.setNumRows(dsHangHoa.size());
        for (int i = 0; i < dsHangHoa.size(); i++) {
            Hanghoa hh = dsHangHoa.get(i);
            dtm.setValueAt(hh.getMahanghoa(), i, 0);
            dtm.setValueAt(hh.getTenhanghoa(), i, 1);
            dtm.setValueAt(hh.getLoaihanghoa().getTenloaihanghoa(), i, 2);
            dtm.setValueAt(hh.getDonvitinh().getTendonvitinh(), i, 3);
            dtm.setValueAt(hh.getGiaban(), i, 4);
            dtm.setValueAt(hh.getGiatoithieu(), i, 5);
            dtm.setValueAt(hh.getGiatoida(), i, 6);
            dtm.setValueAt(hh.getNhaphanphoi().getTennhaphanphoi(), i, 7);
            dtm.setValueAt(hh.getNhasanxuat().getTennhasanxuat(), i, 8);
            dtm.setValueAt(hh.getKhohang().getTenkhohang(), i, 9);
            dtm.setValueAt(hh.getHangton(), i, 10);
            this.tongHangTon+=hh.getHangton();
        }
        //Hiển thị lên Table
        jlbtongHangTon.setText(String.valueOf(this.tongHangTon));
        this.jtbDanhSachHangHoa.setModel(dtm);
        this.jtbDanhSachHangHoa.changeSelection(0, 0, false, false);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jcbKhoHang = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbDanhSachHangHoa = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jlbtongHangTon = new javax.swing.JLabel();
        jbtLoadAll = new javax.swing.JButton();

        setTitle("Thống Kê Hàng Tồn Kho");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jLabel1.setText("Kho hàng:");

        jcbKhoHang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jcbKhoHangMouseReleased(evt);
            }
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jcbKhoHangMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jcbKhoHangMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jcbKhoHangMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jcbKhoHangMousePressed(evt);
            }
        });
        jcbKhoHang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbKhoHangActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Hàng Tồn"));

        jtbDanhSachHangHoa.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbDanhSachHangHoa);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 707, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jButton1.setText("Thoát");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Tổng Hàng Tồn:");

        jlbtongHangTon.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jbtLoadAll.setText("Toàn Bộ Hàng Tồn");
        jbtLoadAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtLoadAllActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jlbtongHangTon, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jcbKhoHang, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtLoadAll)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jcbKhoHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtLoadAll))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addComponent(jLabel2)
                        .addGap(6, 6, 6)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jlbtongHangTon, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        loadcomboxKhoHang();
        hienThiHangHoa();
    }//GEN-LAST:event_formInternalFrameOpened

    private void jcbKhoHangMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jcbKhoHangMouseEntered
        // TODO add your handling code here:
    }//GEN-LAST:event_jcbKhoHangMouseEntered

    private void jcbKhoHangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jcbKhoHangMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jcbKhoHangMouseClicked

    private void jcbKhoHangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbKhoHangActionPerformed
        // TODO add your handling code here:
        hienThiHangTonTheoKhoHang();
    }//GEN-LAST:event_jcbKhoHangActionPerformed

    private void jcbKhoHangMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jcbKhoHangMousePressed
        // TODO add your handling code here:
    }//GEN-LAST:event_jcbKhoHangMousePressed

    private void jcbKhoHangMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jcbKhoHangMouseReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_jcbKhoHangMouseReleased

    private void jcbKhoHangMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jcbKhoHangMouseExited
        // TODO add your handling code here:
    }//GEN-LAST:event_jcbKhoHangMouseExited

    private void jbtLoadAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtLoadAllActionPerformed
        // TODO add your handling code here:
        hienThiHangHoa();
    }//GEN-LAST:event_jbtLoadAllActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtLoadAll;
    private javax.swing.JComboBox jcbKhoHang;
    private javax.swing.JLabel jlbtongHangTon;
    private javax.swing.JTable jtbDanhSachHangHoa;
    // End of variables declaration//GEN-END:variables
}
